package queue;

/**
 * @Author liangzai
 * @Description:
 */
public class MyCircularQueue {
    public int[] elemt;
    public int rear;
    public int front;

    public MyCircularQueue(int k) {
        elemt = new int[k+1];
        rear = 0;
        front = 0;
    }

    public boolean enQueue(int value) {
        if(isFull()){
           return false;
        }
        elemt[rear] = value;
        rear = (rear+1)% elemt.length;
        return true;
    }

    public boolean deQueue() {
        if(isEmpty()){
            return false;
        }
        front = (front+1)% elemt.length;
        return true;
    }

    public int Front() {
        if(isEmpty()){
            return -1;
        }
        int val = elemt[front];
        return val;
    }

    public int Rear() {
        if(isEmpty()){
            return -1;
        }
        int index = (rear == 0)?elemt.length-1:(rear-1)%elemt.length;
        int val = elemt[index];
        return val;
    }

    public boolean isEmpty() {
        if(rear == front){
            return true;
        }
        return false;
    }

    public boolean isFull() {
        if(front == (rear+1)%elemt.length){
            return true;
        }
        return false;
    }
}
